package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysArtist;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.mapper.SysArtistMapper;
import com.ruoyi.system.mapper.SysProjectMapper;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysArtistProjectMapper;
import com.ruoyi.system.domain.SysArtistProject;
import com.ruoyi.system.service.ISysArtistProjectService;

/**
 * 艺人与项目关系Service业务层处理
 * 
 * @author cooc
 * @date 2024-06-19
 */
@Service
public class SysArtistProjectServiceImpl implements ISysArtistProjectService 
{
    @Autowired
    private SysArtistProjectMapper sysArtistProjectMapper;

    @Autowired
    private SysArtistMapper sysArtistMapper;

    @Autowired
    SysArtistServiceImpl sysArtistService;

    @Autowired
    private SysProjectMapper sysProjectMapper;

    /**
     * 查询艺人与项目关系
     * 
     * @param id 艺人与项目关系主键
     * @return 艺人与项目关系
     */
    @Override
    public SysArtistProject selectSysArtistProjectById(Long id)
    {
        SysArtistProject res = sysArtistProjectMapper.selectSysArtistProjectById(id);
        initSysArtistProject(res);
        return res;
    }

    public List<SysArtistProject> selectSysArtistProjectByArtistId(Long id)
    {
        List<SysArtistProject> res = sysArtistProjectMapper.selectSysArtistProjectByArtistId(id);
        if(!res.isEmpty()){
            for (SysArtistProject item : res){
                initSysArtistProject(item);
            }
        }
        return res;
    }

    @Override
    public SysArtistProject selectSysArtistProjectByTowId(Long artistId, Long projectId) {
        SysArtistProject res = sysArtistProjectMapper.selectSysArtistProjectByTowId(artistId,projectId);
        initSysArtistProject(res);
        return res;
    }


    /**
     * 查询艺人与项目关系列表
     * 
     * @param sysArtistProject 艺人与项目关系
     * @return 艺人与项目关系
     */
    @Override
    public List<SysArtistProject> selectSysArtistProjectList(SysArtistProject sysArtistProject)
    {
        List<SysArtistProject> res =  sysArtistProjectMapper.selectSysArtistProjectList(sysArtistProject);
        if(!res.isEmpty()){
           for (SysArtistProject item : res){
               initSysArtistProject(item);
           }
        }
        return  res;
    }

    /**
     * 新增艺人与项目关系
     * 
     * @param sysArtistProject 艺人与项目关系
     * @return 结果
     */
    @Override
    public int insertSysArtistProject(SysArtistProject sysArtistProject)
    {
        return sysArtistProjectMapper.insertSysArtistProject(sysArtistProject);
    }

    /**
     * 修改艺人与项目关系
     * 
     * @param sysArtistProject 艺人与项目关系
     * @return 结果
     */
    @Override
    public int updateSysArtistProject(SysArtistProject sysArtistProject)
    {
        return sysArtistProjectMapper.updateSysArtistProject(sysArtistProject);
    }

    /**
     * 批量删除艺人与项目关系
     * 
     * @param ids 需要删除的艺人与项目关系主键
     * @return 结果
     */
    @Override
    public int deleteSysArtistProjectByIds(Long[] ids)
    {
        return sysArtistProjectMapper.deleteSysArtistProjectByIds(ids);
    }

    /**
     * 删除艺人与项目关系信息
     * 
     * @param id 艺人与项目关系主键
     * @return 结果
     */
    @Override
    public int deleteSysArtistProjectById(Long id)
    {
        return sysArtistProjectMapper.deleteSysArtistProjectById(id);
    }


//    private List<SysArtistProject> filterByLabId(List<SysArtistProject> list,Long labId){
//        List<SysArtistProject> res = new ArrayList<>();
//        if(list != null && !list.isEmpty()){
//            res = list.stream()
//                    .filter(sysArtistProject -> sysArtistProject.getLabs()!=null && Arrays.asList(sysArtistProject.getLabs())
//                    .contains(labId)).collect(Collectors.toList());
//        }
//        return res;
//    }

//    private List<SysArtistProject> filterByLabIds(List<SysArtistProject> list,Long[] labIds){
//        List<SysArtistProject> res = new ArrayList<>();
//        if(labIds==null || labIds.length==0){
//            return list;
//        }else {
//            for (Long projectId : labIds) {
//                res.addAll(filterByLabId(list, projectId));
//            }
//        }
//        return res;
//    }

    public List<SysArtistProject> filterByDimension(List<SysArtistProject> list,String dimension,String comparison,double score){
        return filterByScore(list,dimension,comparison,score);
    }

    public  List<SysArtistProject> filterByScoreTotal(List<SysArtistProject> list,String scoreTotalComparison,double scoreTotal){
        return filterByTotalScore(list,scoreTotalComparison,scoreTotal);
    }

    private List<SysArtistProject> filterByScore(List<SysArtistProject> list,String dimension,String comparison,double score){
        List<SysArtistProject> res = new ArrayList<>();
        if(list!=null && !list.isEmpty()){
            for (SysArtistProject sysArtistProject : list) {
                boolean flag = false;
                JSONArray jsonObjects = JSON.parseArray(sysArtistProject.getProjectScore());
                if(!jsonObjects.isEmpty()){
                   for (Object item:jsonObjects){
                       JSONObject obj = (JSONObject)item;
                       if(obj.containsValue(dimension)){
                           double value = obj.getDoubleValue("value");
                           flag = comparison(value,comparison,score);
                           break;
                       }
                   }
                }
                if(flag){
                    res.add(sysArtistProject);
                }
            }
        }
        return res;
    }

    private List<SysArtistProject> filterByTotalScore(List<SysArtistProject> list,String scoreTotalComparison,double scoreTotal){
        if(!list.isEmpty()){
            return list.stream().filter(item -> comparison(item.getProjectScoreTotal(),scoreTotalComparison,scoreTotal)).collect(Collectors.toList());
        }
        return list;
    }



    private boolean comparison(double value,String comparison,double score){
        comparison = StringEscapeUtils.unescapeXml(comparison);
        long value1 = Double.doubleToLongBits(value);
        long score1 = Double.doubleToLongBits(score);
        switch (comparison){
            case ">":
                return value1>score1;
            case ">=":
                return value1>=score1;
            case "<":
                return value1<score1;
            case "<=":
                return value1<=score1;
            case "=":
                return value1==score1;
            default:
                return false;
        }
    }

    public void  initSysArtistProject(SysArtistProject sysArtistProject){
        final Long id =  sysArtistProject.getProjectId();
        //扩展补充前端显示字段
        //人员信息  后期优化为内部类。不能调service，否则会死循环
        final SysArtist user = sysArtistMapper.selectSysArtistById(sysArtistProject.getArtistId());
        sysArtistService.initSysArtist(user);
        final SysProject project = sysProjectMapper.selectSysProjectById(id);
        if(user!=null){
            sysArtistProject.setSysArtistInfo(user);
        }
        //项目名称
        if(project!=null){
            sysArtistProject.setProjectName(project.getName());
        }
        //逻辑补偿项目相关自定义信息
        if(sysArtistProject.getProjectScore()==null && sysProjectMapper.selectSysProjectById(id)!=null){
           sysArtistProject.setProjectScore(sysProjectMapper.selectSysProjectById(id).getProjectScore());
        }
        if(sysArtistProject.getProjectInfo()==null && sysProjectMapper.selectSysProjectById(id)!=null){
           sysArtistProject.setProjectInfo(sysProjectMapper.selectSysProjectById(id).getProjectInfo());
        }

    }

    public double getScoreTotalValue(String scoreInfo){
        return initScoreTotal(scoreInfo);
    }

    private double initScoreTotal(String scoreInfo) {
        double total = 0;
        if (scoreInfo != null) {
            JSONArray objects = JSON.parseArray(scoreInfo);
            if (!objects.isEmpty()) {
                for (Object item : objects) {
                    JSONObject jsonObject = (JSONObject) item;
                    double value = jsonObject.getDoubleValue("value");
                    total += value;
                }
                return total;
            }
            return total;
        }
        return total;
    }


}
